package com.kaifamiao.demo.d0821;

import java.util.*;
import java.util.function.UnaryOperator;

public class Demo01 {


    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list,  "d", "f", "b", "a", "e", "c");

//        list.sort(null); // Collections.sort(list);

        sort(list);

        System.out.println(list);

//        int c = Collections.binarySearch(list, "c");

        int c = binarySearch1(list, "c");
        System.out.println(c);


        System.out.println(Collections.frequency(list, "a"));

        String max = Collections.max(list);
        System.out.println(max);

        max = Collections.max(list);

        System.out.println(max);

        list.replaceAll(new UnaryOperator<String>() {
            @Override
            public String apply(String s) {
                if (s.equals("a")){
                    return "A";
                }
                return s;
            }
        });

        Collections.replaceAll(list, "b", "B");

        System.out.println(list);

        Collections.rotate(list, 2);
        System.out.println(list);

        Collections.shuffle(list);
        System.out.println(list);

    }

    public static void sort(String[] strs){
        if (strs == null){
            throw new NullPointerException("有病");
        }

        if (strs.length < 2){
            return;
        } else {
            // 排序, 比较  自然比较 Comparable 接口 --> compareTo, 比较器 Comparator  --> compare
            for (int i = 0; i < strs.length - 1; i ++) {
                for (int j = 0; j < strs.length - 1 - i; j ++){
                    if (strs[j].compareTo(strs[j + 1]) > 0){
                        String temp = strs[j];
                        strs[j] = strs[j + 1];
                        strs[j + 1] = temp;
                    }
                }
            }
        }
    }


    public static void sort(List<String> list){
        if (list == null || list.size() < 2){
            return;
        }

        // 排序
        for (int i = 0; i < list.size() - 1; i ++) {
            for (int j = 0; j < list.size() - 1 - i; j ++){
                if (list.get(j).compareTo(list.get(j + 1)) > 0){
                    String temp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, temp);
                    // String temp = list.set(j, list.get(j + 1))  list.set(j + 1, temp);
                }
            }
        }


    }


    public static int binarySearch(List<String> list, String str){
        if (list == null){
            throw new NullPointerException();
        }
        int high = list.size() - 1;
        int low = 0;

        while(low <= high){
            int mid = (low + high) / 2;
            if (str.compareTo(list.get(mid)) < 0){
                high = mid - 1;
            } else if (str.compareTo(list.get(mid)) > 0){
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }


    public static int binarySearch1(List<String> list, String str){
        if (list == null){
            throw new NullPointerException();
        }
        int high = list.size() - 1;
        int low = 0;
        return binarySearch11(list, str, low, high);
    }

    private static int binarySearch11(List<String> list, String str, int low, int high){
        if (low > high){
            return -1;
        }
        int mid = (low + high) / 2;
        if (str.compareTo(list.get(mid)) < 0){
            return binarySearch11(list, str, low, mid - 1);
        } else if (str.compareTo(list.get(mid)) > 0){
            return binarySearch11(list, str, mid + 1, high);
        } else {
            return mid;
        }
    }

}

class Student {

}
